package com.whalefin.service;


import com.whalefin.domain.School;
import com.whalefin.persistence.SchoolMapper;

import com.whalefin.persistence.UserMapper;
import com.whalefin.service.tools.BooleanResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Controller
public class SchoolService {

    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private UserMapper userMapper;


    /**
     * 查询所有开通的学校,根据距离远近排列
     * @param lng 用户经度
     * @param lat 用户纬度
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/searchSchool")
    public @ResponseBody BooleanResult searchSchool(@RequestParam Double lng,@RequestParam Double lat,@RequestParam Integer userId){

        if( lng!=null && lat!=null){
            userMapper.updateUserLocation(lng,lat,userId);
        }
        BigDecimal lngFirst=new BigDecimal(Double.toString(lng));
        Double lngLocation=lngFirst.setScale(6,BigDecimal.ROUND_HALF_UP).doubleValue();//取后六位经度
        BigDecimal latFirst=new BigDecimal(Double.toString(lat));
        Double latLocation=latFirst.setScale(6,BigDecimal.ROUND_HALF_UP).doubleValue();//取后六位纬度
        List list = new ArrayList();
        List<School> schoolList = schoolMapper.queryAllSchool();
        School school = new School();
        Double schoolLng = 0.0;
        Double schoolLat = 0.0;
        for(int i=0;i<schoolList.size();i++){
            school.setId(schoolList.get(i).getId());

            schoolLng = schoolList.get(i).getLng();//学校经度
            schoolLat = schoolList.get(i).getLat();//学校纬度
            Double a = rad(lngLocation) - rad(schoolLng);
            Double b = rad(latLocation) - rad(schoolLat);
            Double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad(lat)) * Math.cos(rad(schoolLat)) * Math.pow(Math.sin(b / 2), 2)));
            s = s*EARTH_RADIUS;
            s = Math.round(s * 10000.0) / 10000.0;
                school.setDistance(s);
                schoolMapper.updateSchool(school);

        }
        List<School> schools = schoolMapper.querySchoolOrderBy();
        BooleanResult booleanResult = new BooleanResult(true,"学校信息");
        booleanResult.getResultMap().put("schoolList",schools);
        return booleanResult;
    }


    /**
     * 查找开通学校
     * @param schoolName
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/searchSchoolByName")
    public @ResponseBody BooleanResult searchSchoolByName(@RequestParam String schoolName){
        List<School> schoolList = schoolMapper.querySchoolByName(schoolName);
        if(schoolList.size() > 0){
            BooleanResult booleanResult = new BooleanResult(true,"学校信息");
            booleanResult.getResultMap().put("schoolMsg",schoolList);
            return booleanResult;
        }else {
            return new BooleanResult(false,"没有相关学校信息");
        }

    }







    //计算地图上两点距离辅助方法
    private static final double EARTH_RADIUS = 6378.137;// 赤道半径(单位m)

    /**
     * 转化为弧度(rad)
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }


}